package GUIMain;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import mail_operations.AdvancedOptionDialog;
import mail_operations.MailerGUI;

import data.ConfigInfo;
import data.Reference;
import fileIO.ConfigIO;

public class MainInterface{
	private static JFrame frame;
	private static JPanel reportPanel;
	private static JPanel editingPanel;
	private static JLabel reportLabel;
	private static JLabel editingLabel;
	private static JLabel originLabel;
	private static JButton originButton;
	private static JLabel editProfileLabel;
	private static JButton editProfileButton;
	private static JLabel editCSLabel;
	private static JButton editCSButton;
	private static JLabel editFieldMappingLabel;
	private static JButton editFieldMappingButton;
	private static GridBagConstraints gbc;
	private static JButton exitButton;
	private static JButton editEmailOptions;
	private static AutoRunGUI autoRunGUI;
	private static OriginReportGUI originReportGUI;
	
	public static void setupGUI(){
		initialize();
		
		gbc.insets.top = 4;
		gbc.insets.bottom = 4;
		gbc.insets.left =4;
		gbc.insets.right = 4;
		
		reportPanel.setLayout(new GridBagLayout());
		reportPanel.setPreferredSize(new Dimension(300,400));
		reportPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.gridx = 0;
		gbc.gridy = 0;
		reportPanel.add(originLabel,gbc);
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 0;
		reportPanel.add(originButton,gbc);
		
		editingPanel.setLayout(new GridBagLayout());
		editingPanel.setPreferredSize(new Dimension(300,400));
		editingPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.gridx = 0;
		gbc.gridy = 0;
		editingPanel.add(editProfileLabel,gbc);
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 0;
		editingPanel.add(editProfileButton,gbc);
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.gridx = 0;
		gbc.gridy = 1;
		editingPanel.add(editCSLabel,gbc);
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 1;
		editingPanel.add(editCSButton,gbc);
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.gridx = 0;
		gbc.gridy = 2;
		editingPanel.add(editFieldMappingLabel,gbc);
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 2;
		editingPanel.add(editFieldMappingButton,gbc);
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.gridx = 0;
		gbc.gridy = 4;
		editingPanel.add(new JLabel("Email Options"),gbc);
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 4;
		editingPanel.add(editEmailOptions,gbc);
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 0;
		gbc.gridy =0;
		frame.add(reportLabel,gbc);
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 0;
		frame.add(editingLabel,gbc);
		
		gbc.gridheight = 3;
		gbc.gridx = 0;
		gbc.gridy = 1;
		frame.add(reportPanel,gbc);
		
		gbc.gridheight = 3;
		gbc.gridx = 1;
		gbc.gridy = 1;
		frame.add(editingPanel,gbc);
		
		JPanel buttonPanel = new JPanel();
		gbc.gridheight = 1;
		gbc.gridx = 0;
		gbc.gridy = 0;
		buttonPanel.add(exitButton,gbc);
		
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		gbc.gridy = 4;
		frame.add(buttonPanel,gbc);
				
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setMinimumSize(frame.getSize());
		GUIFocusManager.add(frame);
		frame.setExtendedState(ConfigInfo.MAXIMIZED);
		frame.setVisible(true);
	}
	private static void initialize(){
		frame = new JFrame("Report Generator");
		frame.setLayout(new GridBagLayout());
		reportPanel = new JPanel();
		reportPanel.setLayout(new GridBagLayout());
		editingPanel = new JPanel();
		editingPanel.setLayout(new GridBagLayout());
		reportLabel = new JLabel(Reference.reportGenerator + " Tools");
		editingLabel = new JLabel("Data Editing Tools");
		originLabel = new JLabel(Reference.reportGenerator);
		originButton = new JButton("Open");
		//File editing stuff
		editProfileLabel = new JLabel("CNEE Profiles");
		editProfileButton = new JButton("Edit");
		editCSLabel = new JLabel("CS Team");
		editCSButton = new JButton("Edit");
		editFieldMappingLabel = new JLabel("Field Mappings");
		editFieldMappingButton = new JButton("Edit");
		editEmailOptions = new JButton("Edit");
		//Other
		exitButton = new JButton("Exit");
		//Windows
		originReportGUI = new OriginReportGUI();
		autoRunGUI = new AutoRunGUI();
		
		attachListeners();
		
		gbc = new GridBagConstraints();
		
		//Initialize system tray
		Runnable runner = new Runnable() {
			 
		     public void run() {
		        if (SystemTray.isSupported()) {
		        	SystemTray tray = SystemTray.getSystemTray();
		          	Image image = Toolkit.getDefaultToolkit().getImage("tcclogo.gif");
		          	
		          	PopupMenu popup = new PopupMenu();
		          	final MenuItem item = new MenuItem("Hide");
		          	
		          	ActionListener a = new ActionListener(){
		          		public void actionPerformed(ActionEvent e){
		          			if(GUIFocusManager.isMinimized())
		          				GUIFocusManager.restoreAll();
		          			else
		          				GUIFocusManager.minimizeAll();
		          		}
		          	};
		          	
		          	final MenuItem restore = new MenuItem("Restore");
		          	restore.addActionListener(new ActionListener(){
		          		public void actionPerformed(ActionEvent e){
		          			GUIFocusManager.restoreAll();
		          		}
		          	});
		        	MenuItem exitItem = new MenuItem("Exit");
		        	exitItem.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent arg0) {
							exit();
						}
		        	});
		          	item.addActionListener(a);
				
		          	final MenuItem endAutoRun = new MenuItem("End Auto Run");
		          	endAutoRun.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e) {
							MainInterface.autoRunGUI.stop();
							((MenuItem)e.getSource()).setEnabled(false);
						}
		          	});
		          	popup.add(item);
		          	popup.add(restore);
		          	popup.add(endAutoRun);
		          	popup.add(exitItem);
		          	TrayIcon trayIcon = new TrayIcon(image, "Report Sender", popup);
		          	
		          	trayIcon.addActionListener(a);
		          	
		          	trayIcon.addMouseListener(new MouseListener(){
						@Override
						public void mouseClicked(MouseEvent arg0) {
							//On mouse click enable/disable end-auto-run
							if(autoRunGUI.isRunning()){
								endAutoRun.setEnabled(true);
							}else{
								endAutoRun.setEnabled(false);
							}
							if(GUIFocusManager.isMinimized()){
								item.setEnabled(false);
								restore.setEnabled(true);
							}else{
								item.setEnabled(true);
								restore.setEnabled(false);
							}
						}
						@Override
						public void mouseEntered(MouseEvent arg0) {
						}
						@Override
						public void mouseExited(MouseEvent arg0) {
						}
						@Override
						public void mousePressed(MouseEvent arg0) {
						}
						@Override
						public void mouseReleased(MouseEvent arg0) {
						}
		          	});
		          	try {
		        	  tray.add(trayIcon);
		          	} catch (AWTException e) {
		        	  System.err.println("Can't add to tray");
		          	}
		        } else {
		        	System.err.println("Tray unavailable");
		        }
		      }
		    };
		    EventQueue.invokeLater(runner);
			frame.setVisible(true);
	}
	public static void attachListeners(){
		editEmailOptions.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				AdvancedOptionDialog.open();
			}
		});
		editProfileButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ProfileEditorGUI.open();
			}
		});
		editCSButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				EmployeeEditor.open(EmployeeEditor.CS_EDITOR);
			}
		});
		editFieldMappingButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				FieldMappingEditorGUI.open();
			}
		});
		exitButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				exit();
			}
		});
		originButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				originReportGUI.open();
			}
		});
		frame.addWindowListener(new WindowListener(){
			@Override
			public void windowActivated(WindowEvent arg0) {
			}
			@Override
			public void windowClosed(WindowEvent arg0) {
			}
			@Override
			public void windowClosing(WindowEvent arg0) {
			}
			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				GUIFocusManager.restoreAll();
			}
			@Override
			public void windowIconified(WindowEvent arg0) {
				//If window is minimized, then send to tray
				GUIFocusManager.minimizeAll();
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
		});
	}
	public static void exit(){
		//If auto-run is active, alert user that it will be terminated
		if(autoRunGUI.isRunning()){
			int selection = JOptionPane.showConfirmDialog(null, "Report auto-run is still enabled, closing the program will terminate \n" +
					"this process.  Do you want still want to close?","Auto-Run Enabled",JOptionPane.YES_NO_OPTION);
			if(selection==JOptionPane.NO_OPTION){
				return;
			}
		}
		//Perform cleanup and saving here
		ConfigInfo.MAXIMIZED = frame.getExtendedState();
		ConfigIO.saveConfig();
		System.exit(0);
	}
	public static AutoRunGUI getAutoRunGUI(){
		return autoRunGUI;
	}
	public static OriginReportGUI getOriginReportGUI(){
		return originReportGUI;
	}
}
